var app = getApp();

let userName = "";
let userGender = "";
let userRegion = "";
let userID = "";
let userClass = "";
let userPhone = "";
let userWeChat = "";
let userQQ = "";
let userIntroduction = "";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    getUserName:{},
    getUserRegion:{},
    getUserID:{},
    getuserClass:{},
    getUserPhone:{},
    getUserWeChat:{},
    getUserQQ:{},
    userGenderArray:["男","女"],
    genderIndex: 0,
    userClass:{},
    getUserRegion: ["北京市", "北京市", "东城区"],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.UserInfo()
  },
  /**
   * 选择用户性别
   */
  bindUserGenderChange(e) {
    console.log(e)
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      genderIndex: e.detail.value
    })
  },
  /**
   * 选择所在地区
   */
  bindUserRegionChange(e) {
    console.log(e)
    
    this.setData({
      getUserRegion: e.detail.value,
    })
  },

  /**
   * 提交保存
   */
  formSubmit (e) {
    console.log(e.detail.value.userName)
    userName = e.detail.value.userName;
    userGender = e.detail.value.userGender;
    userRegion = e.detail.value.userRegion;
    userID = e.detail.value.userID;
    userClass = e.detail.value.userClass;
    userPhone = e.detail.value.userPhone;
    userWeChat = e.detail.value.userWeChat;
    userQQ = e.detail.value.userQQ;
    userIntroduction = e.detail.value.userIntroduction;

    /**
     * 判断用户是否提交为空列表
     */
    if (userName == "" || userID == "" || userPhone == "" || userQQ == "" ||userGender == "" || userID == ""||userClass == "" || userRegion=="" ) {
      console.log(userName,userID,userClass,userPhone,userQQ,userGender)
      wx.showToast({
        title: '请输入完整信息！',
        icon: 'none',
        duration: 2000
      });
    } else if(userID!=app.globalData.sid){
      wx.showToast({
        title: '学号与当前登录账户不符！',
        icon: 'none',
        duration: 2000
      });
    }else{
      console.log(userName,userID,userClass,userPhone,userQQ,userGender)
      this.signUpUserInfo();
    }
  },

  /**
   * 将个人信息保存到数据库
   */
  signUpUserInfo() {
      wx.request({
        method:'POST',
        url: `http://192.168.43.252:8081/api/stu/add?name=${userName}&sex=${userGender}&id=${userID}&classname=${userClass}&phone=${userPhone}&wexin=${userWeChat}&qq=${userQQ}&scrib=${userIntroduction}`,
          // 成功返回
          success: res => {
            console.log('添加成功', res.data);
            let dat = res.data.data;
            wx.showToast({
              title: '正在提交',
              icon: 'loading',
              duration: 1000,
              success: res => {
                wx.showModal({
                  title: dat,
                  content: '期待你的加入！',
                  showCancel: false,
                  success(res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      wx.navigateBack({
                        delta: 1,
                      })
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              },
              fail: err => {
                console.log('保存失败', err);
              },
            })
          },
          fail: err => {
            console.log('添加失败', err);
          },
          complete: res => {
            console.log('添加完成', res)
          }
        })
      },

  
  UserInfo() {
      wx.request({
        url: `http://192.168.43.252:8081/api/wxuser/mess?sid=${app.globalData.sid}`,
        method:'GET',
          // 成功返回
          success: res => {
            console.log(res);
            let dat = res.data;
            if(dat.length!=0){
              for(let i=0;i<dat.length;i++){
                this.setData({
                  getUserName:dat[i].name,
                  getUserID:dat[i].id,
                  getuserClass:dat[i].classname,
                  getUserPhone:dat[i].phone,
                  getUserWeChat:dat[i].wexin,
                  getUserQQ:dat[i].qq,
            })
              if(i==0) break;
          }}else{
              this.setData({
                getUserName:'',
                getUserID:'',
                getuserClass:'',
                getUserPhone:'',
                getUserWeChat:'',
                getUserQQ:'',
          })
            }
            
            
          },
        })
        }

 })
